<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/4
 * Time: 15:28
 */

namespace Admin\Controller;
use Admin\Controller\AdminController;
use Admin\Model\BannerModel;
class BannerController extends AdminController
{
    //查看所有banner图$this->checkPower();
    public function index ()
    {
        $this->checkPower();
        $this->title = "Banner图片列表";
        //查询所有banner图片
        $banner = new BannerModel();
        $bannerList = $banner->queryAllBanner();
        $this->bannerList = $bannerList;

        $this->display("Banner/index");
    }

    //上传banner图片页面$this->checkPower();
    public function addBanner ()
    {
        $this->checkPower();
        $this->title = "上传Banner图片";
        $this->assign("prev",$_SERVER['HTTP_REFERER']);
        $this->display("Banner/add");
    }
    //保存banner图片
    public function saveBanner ()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Banner/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }
        $imageName = $info['photo']['savename'];
        $basepath = $info['photo']['savepath'];
        $ImgName = $this->get_img_url("./Banner/", $imageName, 0);
        //把商品图片进行缩略图显示

        $image = new \Think\Image();
        $image->open($ImgName);
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        //生成保存路径
        $saveImg = "./Banner/" . $basepath;
        //进行缩放
        $_50 = $image->thumb(50, 50)->save($saveImg . "50_" . $imageName);
        //如果有一张缩放失败,就把原图和缩放成功的图片都删掉
        if (!$_50) {
            @unlink($ImgName);
            @unlink($saveImg . "50_" . $imageName);
            return "图片缩放失败";
            die;
        }
        $url = $this->get_img_url($_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST']."/dangjian/Banner/", $imageName, 0);

        $data['name'] = I('post.name');
        $data['width'] = I('post.width');
        $data['height'] = I('post.height');
        $data['is_show'] = I('post.is_show');
        $data['image'] = $imageName;
        $data['url'] = $url;
        $data['create_time'] = date("Y-m-d H:i:s");

        $banner = new BannerModel();
        $res = $banner->saveBanner($data);
        if($res > 0){
            $this->redirect("Banner/index");
        }else{
            $this->error("上传banner图片失败");
        }
    }
    //删除banner图片
    public function delBanner ()
    {

    }
    //更改banner图片是否显示$this->checkPower();
    public function isShow ()
    {
//        $this->checkPower();
        $id = I("get.id");
        $is_show = I("get.show") == 0 ? 1 : 0;
        $banner = new BannerModel();
        $banner->updateShow($id,$is_show);
        $this->redirect("Banner/index");
    }
    //编辑banner图片$this->checkPower();
    public function editBanner ()
    {
        $this->checkPower();
        $this -> title = "banner图片编辑";
        $id = I('get.id');
        $banner = new BannerModel();
        $bannerInfo = $banner->queryBannerById($id);
        $this->bannerInfo = $bannerInfo;
        $this->assign("prev",$_SERVER['HTTP_REFERER']);
        $this->display("Banner/edit");
    }
    //保存编辑图片
    public function saveEditBanner()
    {
        $where['id'] = I("post.id");
        $data['name'] = I("post.name");
        $data['width'] = I("post.width");
        $data['height'] = I("post.height");
        $data['is_show'] = I("post.is_show");
        $data['update_time'] = date("Y-m-d H:i:s");
        $banner = new BannerModel();
        $res = $banner -> saveEditBanner($where,$data);
        if($res > 0){
            $this->redirect("Banner/index");
        }else{
            $this->error("修改失败");
        }
    }
}